﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;
using FlyHorse.LIMS.BLL;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.SamplingPlaceMixForms
{
    public partial class SamplingPlaceMixDetailForm : DevExpress.XtraEditors.XtraForm
    {
        Action _InitGrid;
        int operation;
        SamplingPlaceMix _place;
        SamplingPlaceMixBll placeBll = new SamplingPlaceMixBll();
        public SamplingPlaceMixDetailForm(Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            operation = 1;
            this.Text = ResourceOverall.CreateStr;
        }

        public SamplingPlaceMixDetailForm(SamplingPlaceMix place, Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            operation = 2;
            _place = place;
            this.textEdit1.Text = _place.SamplingPlaceName;
            this.textEdit2.Text = _place.EnglistName;
            this.Text = ResourceOverall.EditStr;
        }

        private void SamplingPlaceMixDetailForm_Load(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.textEdit1.Text == "" || this.textEdit2.Text == "")
            {
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            if (operation == 1)
            {
                using (OJCB4Entities db = new OJCB4Entities())
                {
                    SamplingPlaceMix place = new SamplingPlaceMix();
                    place.SamplingPlaceMixID = Guid.NewGuid().ToString();
                    place.SamplingPlaceName = this.textEdit1.Text;
                    place.EnglistName = this.textEdit2.Text;
                    var entity = db.SamplingPlaceMixes.Add(place);
                    bool res = db.SaveChanges() > 0;
                    if (res)
                    {
                        XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                        _InitGrid();
                        this.Close();
                    }
                }
            }
            else if (operation == 2)
            {
                using (OJCB4Entities db = new OJCB4Entities())
                {
                    SamplingPlaceMix place = db.SamplingPlaceMixes.Find(_place.SamplingPlaceMixID);
                    place.SamplingPlaceName = this.textEdit1.Text;
                    place.EnglistName = this.textEdit2.Text;
                    bool res = db.SaveChanges() > 0;
                    if (res)
                    {
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        _InitGrid();
                        this.Close();
                    }
                }

            }
        }
    }
}